package jk.spider.util.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.log4j.Logger;

public class HDBHelp {
	protected static final Logger log = Logger.getLogger(HDBHelp.class);
	public static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
	public static final String USER = "sa";
	public static final String PASSWORD = "";
	public static final String URL = "jdbc:hsqldb:file:/home/kqy/project/spider/spiderDB/yihaodian/yihaodian";

	protected Connection connection;
	
	public HDBHelp() {
		DbUtils.loadDriver(JDBC_DRIVER);
		try {
			connection = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}  
	}
	
	public List<Map<String, Object>> query(String sql) {
		QueryRunner qr = new QueryRunner();  
		try {
			return (List<Map<String, Object>>) qr.query(connection, sql, new MapListHandler());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public int execInorUpd(String sql) {
		QueryRunner qr = new QueryRunner();  
		try {
			return qr.update(connection, sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public int execInorUpd(String sql, String... params) {
		QueryRunner qr = new QueryRunner();  
		try {
			return qr.update(connection, sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public Object[] select(String sql) {
		ResultSetHandler h = new ResultObjectsHandler();
		QueryRunner qr = new QueryRunner();  
		try {
			return (Object[]) qr.query(connection, sql, h);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
	}
}

